<?php

namespace Home\Model;
use Think\Model;

class OrderModel extends Model {
	protected $tableName = 'orders';
	static public $msg = null;

	//订单详情
	public function getInfo($orderid,$enable = false){
		if(empty($orderid)) return false;
		$where['order_id'] = $orderid;
		if($enable===false){
			$where['enable'] = 1;//可用	
		}
		
		$info = $this->where($where)->find();
		return $info;
	}

	//普通用户订单
	public function getListWithUser($uid=null,$pay_status=null,$page=1,$pagesize=20){
		if(empty($uid)) return null;
		if(!is_numeric($page) || $page <1) $page = 1;
		if(!is_numeric($pagesize) || $pagesize <1) $pagesize = 1;

		$where['enable'] = 1;
		$where['user_id'] = $uid;
		if(!empty($pay_status)){
			if(is_array($pay_status)){
				$where['pay_status'] = array('in',$pay_status);
			}else{
				$where['pay_status'] = $pay_status;
			}
		}

		$sql = "select orders.*,ifnull(user.user_name,0) works_name,ifnull(enter.enterprise_name,0) enter_name,user.auth_type from dmp_orders orders left join dmp_users user on user.id=orders.works_uid left join dmp_enterprises enter on user.enterprise_id=enter.id ";

		$sql .=" where orders.enable=1 and orders.user_id=".$uid;
		if(isset($where['pay_status'])){
			if(is_array($where['pay_status'])){
				$sql .=" and orders.pay_status in (".implode(",",$where['pay_status'][1]).") ";
			}else{
				$sql .=" and orders.pay_status=".$where['pay_status']." ";
			}
		}
		
		$all_list = $this->query($sql);
		$total = count($all_list);

		$sql .=" order by orders.id desc ";
		$offset = ($page-1)*$pagesize;
		$sql .=" limit ".$offset.",".$pagesize;
		$list = $this->query($sql);

		$return['total_page'] = ceil($total/$pagesize);
		$return['list'] = $list;
		$return['current_page'] = $page;
		return $return;
	}

	//服务商用户订单列表
	public function getListWithWorks($works_uid=null,$pay_status=null,$enable = 1,$order_no=null,$start_date=null,$end_date=null,$page=1,$pagesize=20){
		if(empty($works_uid)) return null;
		if(!is_numeric($page) || $page <1) $page = 1;
		if(!is_numeric($pagesize) || $pagesize <1) $pagesize = 1;

		$where['enable'] = $enable;
		$where['works_uid'] = $works_uid;
		if(!empty($pay_status)){
			if(is_array($pay_status)){
				$where['pay_status'] = array('in',$pay_status);
			}else{
				$where['pay_status'] = $pay_status;
			}
		}
		if(!empty($start_date) && !empty($end_date)){
			$where['order_time'] = array('between',array($start_date,$end_date) );
		}else {
			if(!empty($start_date)){
				$where['order_time'] = array('gt',$start_date);
			}else if(!empty($end_date)){
				$where['order_time'] = array('lt',$end_date);
			}
		}
		if(!empty($order_no)){
			$where['order_id'] = $order_no;
		}
		
		$total = $this->where($where)->count();
		$offset = ($page-1)*$pagesize;
		$list = $this->where($where)->limit($offset.",".$pagesize)->order('id desc')->select();

		$return['total_page'] = ceil($total/$pagesize);
		$return['list'] = $list;
		$return['current_page'] = $page;
		return $return;
	}

	//下单
	public function writeOrder($data){
		$this->checkOrderData($data);
		// $data['order_id'] = guid();
		
		return $this->data($data)->add();
	}

	//修改订单
	public function updateOrder($order_id,$data){
		if(empty($order_id)){
			self::$msg = "订单编号缺失";
			return false;
		}
		//付款平台，付款账号信息，pay_time，expire_time，begin_time，pay_status
		
		return false;
		// $User->where('id=5')->save();

	}

	//取消订单
	public function chanelOrder($order_id=null){
		if(empty($order_id)) return false;

		$where['order_id'] = $order_id;
		$data['enable'] = 0;
		return $this->where($where)->data($data)->save();
	}

	//免费api下单记录查询(******有可能是从付费到免费,所以可能有多条记录---待扩展*****)
	public function freeApiSearch($api_id,$service_id,$user_id){
		if(empty($api_id) || !is_numeric($api_id)) return false;
		if(empty($service_id) || !is_numeric($service_id)) return false;
		if(empty($user_id) || !is_numeric($user_id)) return false;
		$where['api_id'] = $api_id;
		$where['service_id'] = $service_id;
		$where['user_id'] = $user_id;

		return $this->where($where)->count();
	}

	//查询用户，某api下单记录（从免费到付费，从付费到免费，付费api购买多个时，付费成功）
	public function searchUserorderWithApi(){

	}

	//校验订单数据
	private function checkOrderData($data){
		if(empty($data)) {
			self::$msg = "无效数据";
			return false;
		}
		if(!isset($data['order_id']) || empty($data['order_id'])){
			self::$msg = "订单编号不正确";
			return false;
		}
		if(!isset($data['api_name_en']) || empty($data['api_name_en'])){
			self::$msg = "API数据不正确";
			return false;
		}
		if(!isset($data['service_name_en']) || empty($data['service_name_en'])){
			self::$msg = "API所属服务信息不正确";
			return false;
		}
		if(!isset($data['user_id']) || empty($data['user_id'])){
			self::$msg = "用户信息缺失";
			return false;
		}

		return true;

	}

	//查询某用户，某api下单，成功，最后的时间---返回时间秒数
	public function getSuccessOrderTime($user_id,$api_id){
		if(empty($user_id) || !is_numeric($user_id)) return 0;
		if(empty($api_id)) return 0;

		$sql = "select expire_time,begin_time from dmp_orders where user_id=".$user_id." and api_id=".$api_id." and pay_status >0  order by id desc limit 1";
		$info = $this->query($sql);
		if(!empty($info)){
			$data = $info[0];
			if($data['expire_time'] < time()){//最后一次购买的产品过期
				return 0;
			}
			$surplus_time = $data['expire_time'] - $data['begin_time'];
			if($surplus_time <=0){
				$surplus_time = 0;
			}
			return $surplus_time;
		}
		return 0;//没有购买

	}

	//查询某用户，某api是否购买
	public function verifyApiWithUid($uid=null,$service_id=null,$api_id=null){
		if(empty($uid) || empty($service_id) || empty($api_id)) return false;

		$where['user_id'] = $uid;
		$where['api_id'] = $api_id;
		$where['service_id'] = $service_id;
		$where['pay_status'] = 1;
		$info = $this->where($where)->order('id desc')->find();
		// echo "<pre>";print_r($info);die;
		if(!empty($info)){
			return 1;
		}else{
			return 0;
		}
	}

	//查询用户订单是否付款成功
	public function wxOrderResult($order_id){
		if(empty($order_id)) return false;

		$where['order_id'] = $order_id;
		$where['pay_status'] = 1;

		$result = $this->where($where)->count();
		return $result;
	}


}

?>